actions.js ➔ deleteComment   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 9
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 9.6666
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A actions.js ➔ ... ➔ api.then 0 3 1
1
import CommentsApi from '../api/restful-api';
2
3 2
let api = new CommentsApi();
4
5 2
export const loadComments = function(context, data) {
6
	return api.getComments(
7
		data.model,
8
		data.modelId,
9
		data.parentId,
10
		typeof data.params === 'object' ? data.params : {}
11
	).then(function(result) {
12
		result.data.comments.forEach((comment) => {
13
			context.commit('addComment', comment)
14
			// Handle child comments if there are any
15
			comment.comments.forEach((comment) => {
16
				context.commit('addComment', comment);
17
18 8
				if (comment.comments !== undefined && comment.comments.length > 0) {
19
					for (var i in comment.comments) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
20
						comment.comments[i].created = new Date(comment.comments[i].created);
21
						context.commit('addComment', comment);
22
					}
23
				}
24
			});
25
26
			context.commit('updatePagination', {
27
				model: data.model,
28
				modelId: data.modelId,
29
				parentId: data.parentId,
30
				pagination: result.data.pagination
31
			});
32
		});
33
	});
34
};
35
36 2
export const deleteComment = function(context, data) {
37
	return api.remove(
38
		data.model,
39
		data.foreign_key,
40
		data.id
41
	).then(function(result) {
42
		context.commit('deleteComment', result.data.comment);
43
	});
44
};
45
46 2
export const updateComment = function(context, data) {
47
	return api.update(
48
		data.model,
49
		data.foreign_key,
50
		data.id,
51
		data
52
	).then(function(result) {
53
		context.commit('addComment', result.data.comment);
54
	});
55
}
56
57 2
export const addComment = function(context, data) {
58
	return api.add(
59
		data.model,
60
		data.foreign_key,
61
		data
62
	).then(function(result) {
63
		context.commit('addComment', result.data.comment);
64
		context.commit('setLastCommentTime');
65
	});
66
}
67